<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script type="text/javascript" src="/layui/layui.js"></script>
</head>
<body>


<div class="layui-container">

    <div style="margin: 0 auto;width: 500px;margin-top: 20%">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">账号：</label>
                <div class="layui-input-inline">
                    <input type="text" name="username"  placeholder="请输入账号"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">密码：</label>
                <div class="layui-input-inline">
                    <input type="password" name="password" placeholder="请输入密码"
                           autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formDemo">登录</button>
                    <button class="layui-btn" lay-submit lay-filter="layerRegister">注册</button>
                </div>
            </div>
        </form>
    </div>
</div>



</body>


<div id="register" style="margin: 0 auto;width: 500px;display:none;margin-top: 20px">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">账号：</label>
            <div class="layui-input-inline">
                <input type="text" name="username"  placeholder="请输入账号"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码：</label>
            <div class="layui-input-inline">
                <input type="password" name="password" placeholder="请输入密码"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="register">注册</button>
            </div>
        </div>
    </form>
</div>

</html>


<script>
    //Demo
    layui.use(['form','layer'], function () {
        var form = layui.form;
        var $= layui.$  //Jquery对象
        var layer =layui.layer;   //弹出层
        //监听提交
        form.on('submit(formDemo)', function (data) {
            console.log(JSON.stringify(data.field))   //对象转换成为字符串

            $.ajax({
                type:'POST',
                url: '/user/login',
                datatype:"json",    //指返回的数据类型
                contentType:"application/json",     //发送到服务器的数据的编码
                data: JSON.stringify(data.field),   //发送到数据库中的数据
                success:function (res) {     //成功后的回调函数  res返回的数据
                    console.log(res)
                    if (res=='ok'){
                        window.location="/index.html"
                    }
                }
            })
            return false;
        });

        //监听layer
        form.on('submit(layerRegister)', function (data) {
            console.log(JSON.stringify(data.field))
            layer.open({    //  layer弹出层
                type: 1,
                title: '用户注册',
                content: $('#register'),
                area: '500px'
            })
            return false;
        });



        //监听注册
        form.on('submit(register)', function (data) {
            console.log(JSON.stringify(data.field))

            $.ajax({
                type:'POST',
                url: '/user/register',
                datatype:"json",
                contentType:"application/json",
                data: JSON.stringify(data.field),
                success:function (res) {
                    console.log(res)
                    if(res.code==0){
                        window.location="/index.html"
                    }
                }
            })
            return false;
        });

    });



</script>